const credential = require('../sql/credentials');

const initData = function (startTime, endTime) {
    let djmp = {};
    startTime = startTime;
    endTime = endTime;
    getAll(startTime, endTime).forEach(v => {
        djmp[v] = {};
        credential.tspzArray.forEach(p => {
            if (p === '单购门票') djmp[v][p] = {};
            else djmp[v][p] = [0, 0]
        });
        credential.dparray.forEach(j => {
            djmp[v]['单购门票'][j] = [0, 0];
        });
        djmp[v]['住宿'] = {};
        credential.ddzs.forEach(o => {
            djmp[v]['住宿'][o] = [0, 0]
        });
        djmp[v]['套餐'] = {};
        credential.tc.forEach(s => {
            djmp[v]['套餐'][s] = [0, 0]
        });
        djmp[v]['单购门票']["其他"] = [0, 0];
        djmp[v]["住宿"]["其他"] = [0, 0];
        djmp[v]["套餐"]["其他"] = [0, 0];
    });
    return djmp
};

Date.prototype.format = function() {
    var s = "";
    var mouth =
        this.getMonth() + 1 >= 10
            ? this.getMonth() + 1
            : "0" + (this.getMonth() + 1);
    var day = this.getDate() >= 10 ? this.getDate() : "0" + this.getDate();
    s += this.getFullYear() + "-"; // 获取年份。
    s += mouth + "-"; // 获取月份。
    s += day; // 获取日。
    return s; // 返回日期。
};

const getAll = function(begin, end) {
    var arr = [];
    var ab = begin.split("-");
    var ae = end.split("-");
    var db = new Date();
    db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
    var de = new Date();
    de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
    var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
    var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
    for (var k = unixDb; k <= unixDe; ) {
        k = k + 24 * 60 * 60 * 1000;
        arr.push(new Date(parseInt(k)).format());
    }
    return arr;
};

const regFun = function(jd, v, m, y, djmp) {
    let regJD = '';
    if (jd.includes("+")) {
        regJD = jd.replace("+", '\\+');
        if (jd.includes("(")) {
            resJD = jd.replace("(", "\\(");
            if (jd.includes(")")) {
                resJD = jd.replace(")", "\\)");
            }
        }
    } else {
        regJD = jd
    }
    let reg = new RegExp(`^${regJD}`);
    if (reg.test(v[y])) {
        djmp[v["Sell_Date"]][m][jd][0] += v.ybrs;
        djmp[v["Sell_Date"]][m][jd][1] += v.yfje;
        return true;
    }
};

const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : "0" + n;
};

const formatTime = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    // const hour = date.getHours();
    // const minute = date.getMinutes();
    // const second = date.getSeconds();

    return (
        [year, month, day].map(formatNumber).join("-")
        // " " +
        // [hour, minute, second].map(formatNumber).join(":")
    );
};

const formatMp = data => {
    const b = new Map();
    const res = [];
    const reg = /([(].*[)])|(-大智禅寺)|(（.*）)|景区/g;
    data.forEach(item => {
        let t = item.ygjd.replace(reg, '');
        if(b.has(t)) {
            b.set(t, [b.get(t)[0]+item.ybrs__sum, b.get(t)[1]+item.gpje__sum])
        }else {
            b.set(t, [item.ybrs__sum, item.gpje__sum])
        }
    });
    b.forEach((value, key)=>{
       res.push({
           'jq': key,
           'ybrs__sum': value[0],
           'gpje__sum': value[1]
       })
    });
    return res
}


module.exports = {initData, getAll, regFun, formatTime, formatMp};